from utils.jsl_data_map import default_columns
from strategy.StrategyTemplate import StrategyTemplate
from strategy import strategy_filter
from strategy.base_strategy.base_strategy_common import *
from utils.db import DbHandler

class StockIncreaseRank(StrategyTemplate):
    """
    正股涨幅策略:
        按正股涨幅排序

    """
    def __init__(self):
        super(StockIncreaseRank, self).__init__()
        self.strategy_name = "按正股涨幅排序策略"
        self.strategy_descrption = "按照正股涨幅从大到小排序"

    def run(self):
        # 基础过滤，过滤掉新债、强赎、EB
        self.data = strategy_filter.filter_base(self.data)
        # 筛选标准
        self.data = self.data.sort_values(by='正股涨跌', ascending=False)
        # 获得默认列
        default_columns.extend(['涨跌幅', '正股涨跌'])
        self.data = self.data[default_columns]
        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)

    def api(self, size=10):
        db = DbHandler()
        db.create_session()
        super(self.__class__, self).api(db=db)
        # sort
        self.data = self.data.sort_values(by='sincrease_rt', ascending=False)
        # 获得默认列
        self.data = self.data[common_column][:size]

        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)
        self.db.close_session()
        return self.data

if __name__ == '__main__':
    ins = StockIncreaseRank()
    ins.topN = 20
    ins.test()

